<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>javascript 阶段</title>
    <style>
        .demo {
            width: 200px;
            height: 50px;
            background-color: orange;
            border: 2px solid black;
            position: relative;
            text-align: center;
            font-size: 20px;
            line-height: 50px;
        }

        button {
            position: absolute;
            outline: none;
            width: 100px;
            height: 50px;
            left: 250px;
            top: 0;
            cursor: pointer;
        }

        .active {
            background-color: white;
        }
        
    </style>
</head>
<body>
    <div class="demo demo_1">9楼<button data-index="0">按钮</button></div>
    <div class="demo demo_2">8楼<button data-index="1">按钮</button></div>
    <div class="demo demo_3">7楼<button data-index="2">按钮</button></div>
    <div class="demo demo_4">6楼<button data-index="3">按钮</button></div>
    <div class="demo demo_5">5楼<button data-index="4">按钮</button></div>
    <div class="demo demo_6">4楼<button data-index="5">按钮</button></div>
    <div class="demo demo_7">3楼<button data-index="6">按钮</button></div>
    <div class="demo demo_8">2楼<button data-index="7">按钮</button></div>
    <div class="demo demo_9">1楼<button data-index="8">按钮</button></div>
    <script>
        var demo = document.querySelectorAll(".demo");
        var button = document.querySelectorAll("button");
        for(var i = 0 ; i < button.length ; i ++) {
            button[i].onclick = function () {
                var index = this.dataset.index;
                demo[index].classList.toggle("active");
                button[index].classList.toggle("active");
            }
        }
    </script>
</body>
</html>